package com.eye.catcher.controller;


import com.eye.catcher.page.BasePage;
import com.eye.catcher.param.CommonResponse;
import com.eye.catcher.param.ResponseData;
import com.eye.catcher.param.ResponseUtil;
import com.eye.catcher.service.ITCodDaDictionaryService;
import com.eye.catcher.system.dictionary.param.DictionaryDto;
import com.eye.catcher.constants.SbCode;
import com.eye.catcher.rabbit.MQDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huzhiwei
 * @since 2022-02-18
 */
@Slf4j
@RestController
@RequestMapping("/eyeCatcher-dictionary/dictionary")
public class TCodDaDictionaryController extends TSysDaBaseController {


    @Autowired
    private ITCodDaDictionaryService daDictionaryService;

    @PostMapping("/addGeneral")
    @ResponseBody
    public ResponseData addGeneral(@RequestBody DictionaryDto dictionary) {
        String methodCode = "/addGeneral";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_ADD,dictionary.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.daDictionaryService.add(dictionary);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    @PostMapping("/editGeneral")
    @ResponseBody
    public ResponseData editGeneral(@RequestBody DictionaryDto dictionary) {
        String methodCode = "/editGeneral";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_ADD,dictionary.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.daDictionaryService.edit(dictionary);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    @PostMapping("/deleteGeneral")
    @ResponseBody
    public ResponseData deleteGeneral(@RequestBody DictionaryDto dictionary) {
        String methodCode = "/deleteGeneral";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_DELETE,dictionary.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.daDictionaryService.deleteByCode(dictionary);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        } finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    //获取所有表缓存json
    @GetMapping("/getCacheDictionaryJson")
    @ResponseBody
    public ResponseData getCacheDictionaryJson() {
        String methodCode = "/getCacheDictionaryJson";
        try{
            commonResponse = CommonResponse.success();
            commonResponse.setData(this.daDictionaryService.getCacheDictionaryJson());
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    @PostMapping("/getGeneralList")
    @ResponseBody
    public ResponseData getGeneralList(@RequestBody DictionaryDto dictionary) {
        String methodCode = "/getGeneralList";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,
                    getClass(),SbCode.OPERATION_QUERY,dictionary.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.daDictionaryService.getGeneralList(dictionary)
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    @PostMapping("/getGeneralPageList")
    @ResponseBody
    public ResponseData getGeneralPageList(@RequestBody DictionaryDto dictionary) {
        String methodCode = "/getGeneralPageList";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,
                    getClass(),SbCode.OPERATION_QUERY,dictionary.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            commonResponse = CommonResponse.success();
            BasePage page = this.getPageInfo(dictionary);
            dictionary.setPageNum(String.valueOf(page.getPageNum()));
            dictionary.setPageSize(String.valueOf(page.getPageSize()));
            dictionary.setStartSize(String.valueOf((page.getPageNum()-1) * page.getPageSize()));
            dictionary.setEndSize(String.valueOf((page.getPageNum()-1) * page.getPageSize() + page.getPageSize()));

            commonResponse.setData(this.daDictionaryService.getGeneralPageList(dictionary));
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
}
